﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApp5
{
    class 文件夹
    {
        public 文件夹()
        {
            //MyDirectory myDirectory = new MyDirectory();
            MyDirectoryInfo myDirectoryInfo = new MyDirectoryInfo();
        }
        class MyDirectoryInfo
        {
            public MyDirectoryInfo()
            {
                DirectoryInfo directory = new DirectoryInfo(Directory.GetCurrentDirectory());
                directory.GetDirectories();
                // 创建文件夹
                if (directory.Exists)
                    Console.WriteLine("文件夹存在");
                // 创建子目录
                DirectoryInfo subDir = directory.CreateSubdirectory("files");
                Console.WriteLine("子目录：" + subDir.Name);
                //subDir.Create();    // 创建目录，如果已经有该目录不会报错，不会更改里面的内容
                subDir.Refresh();
                if (Directory.Exists("./MyFiles"))
                    Directory.Delete("MyFiles");
                subDir.MoveTo("./MyFiles");

                Console.WriteLine("文件夹：");
                DirectoryInfo[] direcrotyInfos = directory.GetDirectories();
                for (int i = 0; i < direcrotyInfos.Length; i++)
                {
                    Console.WriteLine(direcrotyInfos[i].FullName);
                }
                Console.WriteLine("文件：");
                FileInfo[] fileInfos = directory.GetFiles();
                foreach (FileInfo info in fileInfos)
                    Console.WriteLine(info.FullName);
                Console.WriteLine("文件和文件夹：");
                FileSystemInfo[] fileSystemInfo = directory.GetFileSystemInfos();
                foreach (FileSystemInfo info in fileSystemInfo)
                    Console.WriteLine(info.FullName);
            }
        }
        class MyDirectory
        {
            public MyDirectory()
            {
                string currentDir = Directory.GetCurrentDirectory();

                if (Directory.Exists("files"))
                    Directory.Delete("files", true);        // 不写第二个参数则无法删除里面的文件，会运行报错
                // 创建文件夹
                Directory.CreateDirectory("files");
                Directory.CreateDirectory("test");
                // 创建文件
                if (!File.Exists("files/text.txt"))
                    File.Create("files/text.txt");
                // 移动文件夹
                if (!Directory.Exists("test"))
                    Directory.Move("files", "test");            // 文件夹已经存在则无法移动

                Console.WriteLine("当前文件夹有哪些目录");
                string[] dirs = Directory.GetDirectories(currentDir);
                foreach (string str in dirs)
                {
                    Console.WriteLine(str);
                }
                Console.WriteLine("当前文件夹有哪些文件");
                string[] files = Directory.GetFiles(currentDir);
                foreach (string str in files)
                {
                    Console.WriteLine(str);
                }
                Console.WriteLine("当前文件夹有哪些文件和文件夹");
                string[] fileEntries = Directory.GetFileSystemEntries(currentDir);
                foreach (string str in fileEntries)
                {
                    Console.WriteLine(str);
                }
            }
        }
    }
}
